<?php
namespace addons\huochetou\controller;
use think\Request;
use think\Loader;
use think\Db;
use app\common\controller\Init;
class ApiUserHuochetou extends Init 
{
    public function articleAdd()
    {
        if (!Request::instance() -> isPost()) {
            exit('提交方式错误');
        }
        $password = input('post.password');
        $password = strval($password);
        $collectPassword = db('BaseKey')->where('key','huochetou')->find()['val'];
        $collectPassword = strval($collectPassword);
        if ($password != $collectPassword) {
            exit('密码错误');
        }
        
        $title = input('post.title');
        $content = input('post.content');
        if (!$title) {
            exit('请输入标题');
        }
        if (!$content) {
            exit('请输入内容');
        }
        
        $keywords = input('post.keywords');
        $description = input('post.description');
        $cid = input('post.cid');
        $imgUrl = input('post.imgUrl');
        $tags = input('post.tags');
        $publish_time = input('post.publish_time') ? input('post.publish_time') : time();;
        $cid = $cid ? $cid : 0;
        $resArray = array(
            'title'=> $title,
            'cid' => $cid,
            'publish_time' => $publish_time,
            'keywords' => $keywords,
            'description' => $description,
            'img_url' => $imgUrl,
            'content' => $content,
            'tags' => $tags,
        );
        
        $allPost = input();
        $fieldList = [];
        if ($allPost) {
        	foreach ($allPost as $key => $val) {
    			if (strpos($key,'diy_') !== false) {
    				$fieldList[] = ['field' => $key,'value' => $val];
    			}
        	}
        }
        $indexId = model('app\article\model\Articles')->itemAdd($resArray,$fieldList);
        if ($indexId) {
            exit('发布成功');
        } else {
            exit('发布失败，请检查标题是否重复');
        }

    }
}

